﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using FSY.API.Utilities;
using FSY.API.Files;
using FSY.Components.Entities;

namespace FSY.Core.UI
{
    public class LoadSkin : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //Xu ly load skin o day            
            try
            {
                Control ctrSkin = null;
                int tabid = -1;
                string pathSkin = string.Empty;
                Fsy_Theme theme = null;
                Box boxModule = null;

                #region Load Skin
                if (Request.Params["TabID"] != null)
                {
                    tabid = Fsy_Null.SetNullInteger(Request.Params["TabID"]);
                }
                if (tabid <= 0)
                {
                    theme = Fsy_ThemeController.GetThemeIsDefault();                   
                }
                else
                {
                    Fsy_Menu menu = Fsy_MenuController.GetMenuByID(tabid);
                    theme = Fsy_ThemeController.GetThemeByID(menu.ThemeID);                    
                }
                if (theme != null)
                {
                    pathSkin = Fsy_FileController.GetVitualFilePath("Themes/" + theme.ThemeName + "/Skin.ascx");
                }

                ctrSkin = LoadControl(pathSkin);
                pathSkin = string.Empty;
                #endregion

                //Dang ky JS va CSS

                //---------------------

                //Xu ly load module
                List<Fsy_MenuModule> lsMenuModule = GetListModule(tabid);
                //-----------------
                List<Position> lsPanel = GetControlPanel(ctrSkin);
                foreach (Position pos in lsPanel)
                {
                    foreach (Fsy_MenuModule module in lsMenuModule)
                    {
                        #region Load box cho module
                        boxModule = (Box)this.LoadControl(Fsy_FileController.GetVitualFilePath("Themes/" + module.BoxName));
                        boxModule.BoxContent = this.LoadControl(Fsy_FileController.GetVitualFilePath("DesktopModules/" + module.ControlTitle + "/" + module.ControlSrc));
                        if (module.ShowTitle == true)
                        {
                            //Xu ly ngon ngu hien thi o day tam thoi lay Control Title cua Module
                            boxModule.Title = module.ControlTitle;
                        }
                        #endregion----------------------

                        #region Add boxModule vao vi tri theo thu tu tren panel Position
                        if (module.ViewOrder >= pos.Controls.Count)
                        {
                            //pos.Controls.Add(LoadControl(Fsy_FileController.GetVitualFilePath("Modules/" + module.ControlTitle + "/" + module.ControlSrc)));
                            pos.Controls.Add(boxModule);
                        }
                        else
                        {
                            //pos.Controls.AddAt(module.ViewOrder,LoadControl(Fsy_FileController.GetVitualFilePath("Modules/" + module.ControlTitle + "/" + module.ControlSrc)));
                            pos.Controls.AddAt(module.ViewOrder, boxModule);
                        }
                        #endregion
                    }
                }
                this.Controls.Add(ctrSkin);
            }
            catch
            {
            }
        }

        #region Method Private
        private List<Fsy_MenuModule> GetListModule(int menuID)
        {
            List<Fsy_MenuModule> ls = Fsy_MenuModuleController.GetMenuModuleByMenuID(menuID);
            foreach (Fsy_MenuModule mm in ls)
            {
                if (mm.IsSecure == true)
                {
                    if (Session["UserID"] != null)
                    {
                        int userID = Fsy_Null.SetNullInteger(Session["UserID"]);
                        List<Fsy_RoleQuery> lsRole = Fsy_RoleController.GetRoleByUser(userID);
                        if (lsRole.Count > 0)
                        {
                            foreach (Fsy_RoleQuery role in lsRole)
                            {
                                if (Fsy_MenuModuleController.IsAuthorization(userID, role.RoleID) == false)
                                    ls.Remove(mm);
                            }
                        }
                    }
                }
            }
            return ls;
        }
        private List<Position> GetControlPanel(Control ctrlSkin)
        {
            List<Position> lsPos = new List<Position>();
            foreach (Control panel in ctrlSkin.Controls)
            {
                if (panel.GetType() == typeof(Position))
                {
                    lsPos.Add((Position)panel);
                }
            }
            return lsPos;
        }
        #endregion
    }
}
